package com.blog.controller;

import com.blog.pojo.Result;
import com.blog.service.ArticleService;
import com.blog.pojo.Article;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @PostMapping("/add")
    public Result addArticle(@RequestBody Article article){
        if (article==null) {
            return null;
        }
        try {
            articleService.addArticle(article);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,null,0L);
        }
        return new Result(true,null,0L);
    }
    @GetMapping("/list")
    public Result list(@RequestParam int limit,@RequestParam int now_page){
        List<Article> list;
        Long total;
        try {
           list = articleService.list(limit,now_page);
            total=articleService.count();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,null,0L);
        }
        return new Result(true,list,total);
    }
    @GetMapping("/detail/{id}")
    public Article detail(@PathVariable int id){
        Article detail;
        try {
            detail = articleService.detail(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return detail;
    }
    @PostMapping("/edit")
    public boolean edit(@RequestBody Article article){
        try {
            articleService.edit(article);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
    @GetMapping("/delete/{id}")
    public void delete(@PathVariable int id){
        articleService.delete(id);
    }

}
